package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SignCalc;
import cn.wolfcode.trip.base.domain.SignDetail;
import cn.wolfcode.trip.base.mapper.SignCalcMapper;
import cn.wolfcode.trip.base.mapper.SignDetailMapper;
import cn.wolfcode.trip.base.service.ISignCalcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SignCalcServiceImpl implements ISignCalcService {
    @Autowired
    private SignCalcMapper signCalcMapper;
    @Autowired
    private SignDetailMapper signDetailMapper;

    @Override
    public SignCalc queryYesterdayByUserId(Long userId) {
        //判断id是否为null
        if (userId==null) {
            throw new RuntimeException("未登录,请先登录帐号");
        }
        SignCalc SignCalc =signCalcMapper.selectYesterDayByUserId(userId);
        return SignCalc;
    }

    @Override
    public SignCalc todaySign(Long userId) {
        //判断id是否为null
        if (userId==null) {
            throw new RuntimeException("未登录,请先登录帐号");
        }//查询今天是否有签到
        SignCalc today = signCalcMapper.selectTodaySign(userId);
        return today;
    }

    @Override
    public void addSign(SignCalc signCalc) {
        signCalcMapper.insert(signCalc);
    }

    @Override
    public SignCalc getSignRecord(Long userId) {
        return signCalcMapper.selectYesterDayByUserId(userId);
    }

    @Override
    public List<String> selectListDay(Long userId,String yearAndMoth) {
        List<String> day = signCalcMapper.selectDay(userId,yearAndMoth);
        return day;
    }

    @Override
    public SignDetail queryGrade(Long userId) {
        return signDetailMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void addGrade(SignDetail signDetail) {
        signDetailMapper.insert(signDetail);
    }

    @Override
    public void updateGrade(SignDetail signDetail) {
        signDetailMapper.updateByPrimaryKey(signDetail);
    }

    @Override
    public Integer querySuccRecord(Long userId) {
        return signCalcMapper.selectSuccRerocdByUserId(userId);
    }

    @Override
    public void updateSuccRecord(Long userId,Integer succRecord) {
        signCalcMapper.updateSuccRecord(userId,succRecord);
    }

    @Override
    public Boolean queryFirst(Long userId) {
        List<SignCalc> signCalcs = signCalcMapper.selectFirst(userId);
        if (signCalcs.isEmpty()) {
            return false;
        }
        return true;
    }

    @Override
    public void save(Long userId) {
        SignCalc signCalc = new SignCalc();
        SignDetail signDetail = new SignDetail();
        //添加默认的签到表格和积分表格
        signCalc.setUserId(userId);
        signCalc.setSuccRecord(0);
        signCalc.setSignData(null);
        signDetail.setUserId(userId);
        signDetail.setGrade(0L);
        signCalcMapper.insert(signCalc);
        signDetailMapper.insert(signDetail);
    }


}
